package com.cqut.forum.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.Article;
import com.cqut.forum.entity.ArticleReply;
import com.cqut.forum.entity.User;
import com.cqut.forum.service.ActiveService;
import com.cqut.forum.service.ArticleReplyService;
import com.cqut.forum.service.ModuleService;
import com.cqut.forum.util.PackTo;

/**
 * 用户心情controller
 * @author yangxun
 */
@Controller
public class UserEventAction {
	private String userActive;
	
	@Resource
	private ActiveService activeService;
	@Resource
	private ModuleService moduleService;
	@Resource
	private ArticleReplyService ars;
	
	@RequestMapping("/user/user_active")
	@ResponseBody
	public Map<String, Object> userEvent(HttpServletRequest request,String userActive){
		User user = (User) request.getSession().getAttribute("user");
		if(user == null){
			return null;
		}
		Article article = PackTo.packArticle(user.getUserId(), userActive);
		article.setModuleId(moduleService.getModuleIndex("动态"));
		
		article = activeService.addActive(article);
		//封装成json格式的数据传送到客户端
		Map<String, Object> map = PackTo.packActive(user,article);
		return map;
	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping("/user/article_reply")
	@ResponseBody
	public Map<String, Object> articleReply(HttpServletRequest request,String[] replyInfo){
		User user = (User) request.getSession().getAttribute("user");
		if(user == null){
			return null;
		}
		Article article = activeService.getArticle(Integer.parseInt(replyInfo[0]));
		article.setLastReplyTime(new Date());
		//更新最后评论时间
		article = activeService.updateArticle(article);
		
		ArticleReply ar = new ArticleReply();
		ar.setUserId(user.getUserId());
		ar.setArticleReplyTime(new Date());
		ar.setArticleReplyState(true);
		ar.setArticleReplyContents(replyInfo[1]);
		ar.setArticle(article);
		//保存评论
		ar = ars.save(ar);
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("image", user.getUserPhoto());
		result.put("name", user.getUserName());
		result.put("content", ar.getArticleReplyContents());
		result.put("time", ar.getArticleReplyTime().toLocaleString());
		return result;
	}
	/**************get and set method***************/
	public String getUserActive() {
		return userActive;
	}
	public void setUserActive(String userActive) {
		this.userActive = userActive;
	}
	//183
	//13542065255
}
